package com.chujian.system.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chujian.framework.core.base.BaseServiceImpl;
import com.chujian.core.entity.CoreCfg;
import com.chujian.system.core.mapper.CoreCfgMapper;
import com.chujian.system.core.service.CoreCfgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 配置表 服务实现类
 *
 * @author yj
 * @since 2021-07-28
 */
@Service
@Slf4j
public class CoreCfgServiceImpl extends BaseServiceImpl<CoreCfgMapper, CoreCfg> implements CoreCfgService {

    @Autowired
    @Lazy
    private CoreCfgService cfgService;

    @Override
    public CoreCfg saveAcConfig(Long acId, CoreCfg cfg) {
        CoreCfg e = this.getOne(acId, cfg.getCfgKey());
        if (null == e) {
            cfgService.save(cfg);
        } else {
            cfg.setCfgId(e.getCfgId());
            cfgService.updateById(cfg);
        }
        return cfg;
    }

    @Override
    public CoreCfg getOne(Long acId, String cfgKey) {
        return this.getOne(newWrapper(acId, cfgKey));
    }

    @Override
    public List<CoreCfg> list(Long acId, String cfgKey) {
        return this.list(newWrapper(acId, cfgKey));
    }

    private QueryWrapper<CoreCfg> newWrapper(Long acId, String cfgKey) {
        return new QueryWrapper<CoreCfg>().eq("cfg_key", cfgKey).eq("ac_id", acId);
    }
}
